﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ueEngine.Framework.Caching;

namespace ueEngine.Web.WebLogic.PageDataProxy
{
    public abstract class IDataProxy<T> where T:class
    {
        protected string CachePrefix = "PageProxy:";
        public static IDataProxy<T> GetHandle(PageCacheType type)
        {
            switch (type)
            {
                case PageCacheType.HomeIndex:
                    return new HomeIndex<T>() as IDataProxy<T>;
                case PageCacheType.HomeJobDetail:
                    return new HomeJobDetail<T>() as IDataProxy<T>;
                case PageCacheType.PositionListQuery:
                    return new PositionListQuery<T>() as IDataProxy<T>;
                default:
                    throw new Exception("数据代理枚举类型无效");
            }
        }
        public T GetData(params string[] keys)
        {
            if (CheckedParams(keys))
            {
                T data = ueCache.GetCache<T>(BuildKey(keys));
                if (data == null)
                {
                    data = BuildRefrashAction(keys);
                    if (data != null)
                    {
                        return ueCache.Add<T>(BuildKey(keys), data, CacheExpireStrategy.Absolute, new TimeSpan(0, 10, 0));
                    }
                }
                return data;
            }
            throw new Exception("缓存获取失败，参数不正确");
        }
        protected abstract bool CheckedParams(params string[] keys);
        protected abstract string BuildKey(params string[] keys);
        protected abstract T BuildRefrashAction(params string[] keys);
    }
    public enum PageCacheType
    {
        HomeIndex,
        HomeJobDetail,
        PositionListQuery
    }
}
